/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.tools;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Lam
 */
public class CommonPhotoTools {

    /**
     * 主要是用base64解码与编码
     */
    public static boolean BASE64Decoding(String str, String imgType, String path) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] ib = decoder.decodeBuffer(str);
        BufferedImage bi = ImageIO.read(new ByteArrayInputStream(ib));
        boolean isFinish = ImageIO.write(bi, imgType, new File(path));
        bi.flush();
        return isFinish;
    }
    
    public static byte[] Str2byte(String str){
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] ib=null;
        try {
            ib = decoder.decodeBuffer(str);
        } catch (IOException ex) {
            System.out.println("字符转二进制出错");
            Logger.getLogger(CommonPhotoTools.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ib;
    }

    public static String BASE64Encodeing(String path) throws Exception {
        BASE64Encoder encoder = new BASE64Encoder();
        File file = new File(path);
        FileInputStream fis = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        fis.read(buffer);
        return encoder.encode(buffer);
    }
}
